function PPublishUtils() {
};

PPublishUtils.number = 20;

PPublishUtils.normal = 1;
PPublishUtils.dop = 2;
PPublishUtils.mixed = 3;
PPublishUtils.none = 4;

PPublishUtils.viewallSmryArgs = function(smryGui, user) {
  var cond1 = "d.is_canceled = 0 AND ((d.state_id = 2 OR d.state_id = 3 OR d.state_id = 4 OR d.state_id = 5 OR d.state_id = 6 OR d.state_id = 7 OR d.state_id = 8 OR d.state_id = 9 OR d.state_id = 10 OR d.state_id = 11 OR d.state_id = 12 OR d.state_id = 13 OR d.state_id = 14 OR d.state_id = 15 OR d.state_id = 16 OR d.state_id = 17) OR (((d.state_id = 19 OR d.state_id = 21 ) AND d.custom_assigner_id = " + user.id + ") OR ((d.state_id = 18 OR d.state_id = 20) AND d.custom_assignee_id = " + user.id + ")))";
  var cond2 = "d.is_canceled = 0 AND (d.state_id = 2)";
  var cond3 = "d.is_canceled = 0 AND (d.state_id = 3 OR d.state_id = 5)";
  var cond16 = "d.is_canceled = 0 AND (d.state_id = 4)";
  var cond4 = "d.is_canceled = 0 AND (d.state_id = 6)";
  var cond17 = "d.is_canceled = 0 AND (d.state_id = 7)";
  var cond5 = "d.is_canceled = 0 AND (d.state_id = 8)";
  var cond6 = "d.is_canceled = 0 AND (d.state_id = 9)";
  var cond7 = "d.is_canceled = 0 AND (d.state_id = 10)";
  var cond8 = "d.is_canceled = 0 AND (d.state_id = 11)";
  var cond9 = "d.is_canceled = 0 AND (d.state_id = 12)";
  var cond10 = "d.is_canceled = 0 AND (d.state_id = 13)";
  var cond11 = "d.is_canceled = 0 AND (d.state_id = 14)";
  var cond12 = "d.is_canceled = 0 AND (d.state_id = 15)";
  var cond13 = "d.is_canceled = 0 AND (d.state_id = 16)";
  var cond14 = "d.is_canceled = 0 AND (d.state_id = 17)";
  var cond15 = "d.is_canceled = 0 AND (((d.state_id = 19 OR d.state_id = 21 ) AND d.custom_assigner_id = " + user.id + ") OR ((d.state_id = 18 OR d.state_id = 20) AND d.custom_assignee_id = " + user.id + ")))";

  if (smryGui) {
    var smryArgs = [ { name: smryGui.allTasks.id, cond: cond1 },
                     { name: smryGui.backup.id, cond: cond2 },
                     { name: smryGui.check.id, cond: cond3 },
                     { name: smryGui.nocheck.id, cond: cond16 },
                     { name: smryGui.checkPhoto.id, cond: cond4 },
                     { name: smryGui.nocheckphoto.id, cond: cond17 },
                     { name: smryGui.pickup.id, cond: cond5 },
                     { name: smryGui.complete.id, cond: cond6 },
                     { name: smryGui.checkSort.id, cond: cond7 },
                     { name: smryGui.author.id, cond: cond8 },
                     { name: smryGui.rauthor.id, cond: cond9 },
                     { name: smryGui.publish.id, cond: cond10 },
                     { name: smryGui.checkPublish.id, cond: cond11 },
                     { name: smryGui.published.id, cond: cond12 },
                     { name: smryGui.unreceived.id, cond: cond13 },
                     { name: smryGui.received.id, cond: cond14 },
                     { name: smryGui.personal.id, cond: cond15 }
                   ];
    return smryArgs;
  }
  return null;
  
}

PPublishUtils.asstSmryArgs = function(smryGui, user) {
  var cond1 = "d.is_canceled = 0 AND ((d.state_id = 2 OR d.state_id = 3 OR d.state_id = 4 OR d.state_id = 5 OR d.state_id = 8 OR d.state_id = 11 OR d.state_id = 12 OR d.state_id = 13 OR d.state_id = 16) OR (((d.state_id = 19 OR d.state_id = 21 ) AND d.custom_assigner_id = " + user.id + ") OR ((d.state_id = 18 OR d.state_id = 20) AND d.custom_assignee_id = " + user.id + ")))";
  var cond2 = "d.is_canceled = 0 AND (d.state_id = 2)";
  var cond3 = "d.is_canceled = 0 AND (d.state_id = 3 OR d.state_id = 5)";
  var cond10 = "d.is_canceled = 0 AND (d.state_id = 4)";
  var cond4 = "d.is_canceled = 0 AND (d.state_id = 8)";
  var cond5 = "d.is_canceled = 0 AND (d.state_id = 11)";
  var cond6 = "d.is_canceled = 0 AND (d.state_id = 12)";
  var cond7 = "d.is_canceled = 0 AND (d.state_id = 13)";
  var cond8 = "d.is_canceled = 0 AND (d.state_id = 16)";
  var cond9 = "d.is_canceled = 0 AND (((d.state_id = 19 OR d.state_id = 21 ) AND d.custom_assigner_id = " + user.id + ") OR ((d.state_id = 18 OR d.state_id = 20) AND d.custom_assignee_id = " + user.id + ")))";

  if (smryGui) {
    var smryArgs = [ { name: smryGui.allTasks.id, cond: cond1 },
                     { name: smryGui.backup.id, cond: cond2 },
                     { name: smryGui.check.id, cond: cond3 },
                     { name: smryGui.nocheck.id, cond: cond10 },
                     { name: smryGui.pickup.id, cond: cond4 },
                     { name: smryGui.author.id, cond: cond5 },
                     { name: smryGui.rauthor.id, cond: cond6 },
                     { name: smryGui.publish.id, cond: cond7 },
                     { name: smryGui.receive.id, cond: cond8 },
                     { name: smryGui.personal.id, cond: cond9 }
                   ];
    return smryArgs;
  }
  return null;
};

PPublishUtils.adminSmryArgs = function(smryGui, user) {
  var cond1 =  "d.is_canceled = 0 AND ((d.state_id = 6 OR d.state_id = 7 OR d.state_id = 10 OR d.state_id = 14 OR d.state_id = 16) OR (((d.state_id = 19 OR d.state_id = 21 ) AND d.custom_assigner_id = " + user.id + ") OR ((d.state_id = 18 OR d.state_id = 20) AND d.custom_assignee_id = " + user.id + ")))";
  var cond2 = "d.is_canceled = 0 AND (d.state_id = 6 OR d.state_id = 7)";
  var cond3 = "d.is_canceled = 0 AND d.state_id = 10";
  var cond4 = "d.is_canceled = 0 AND (d.state_id = 14)";
  var cond5 = "d.is_canceled = 0 AND (d.state_id = 16)";
  var cond6 = "d.is_canceled = 0 AND (((d.state_id = 19 OR d.state_id = 21 ) AND d.custom_assigner_id = " + user.id + ") OR ((d.state_id = 18 OR d.state_id = 20) AND d.custom_assignee_id = " + user.id + ")))";

  if (smryGui) {
    var smryArgs = [ { name: smryGui.allTasks.id, cond: cond1 },
                     { name: smryGui.checkPhoto.id, cond: cond2 },
                     { name: smryGui.checkSort.id, cond: cond3 },
                     { name: smryGui.checkPublish.id, cond: cond4 },
                     { name: smryGui.receive.id, cond: cond5 },
                     { name: smryGui.personal.id, cond: cond6 }
                   ];
    return smryArgs;
  }
  return null;
};

PPublishUtils.desSmryArgs = function(smryGui, user) {
  var cond1 = "d.is_canceled = 0 AND ((d.state_id = 9 AND d.designer_id = " + user.id + ") OR (((d.state_id = 19 OR d.state_id = 21 ) AND d.custom_assigner_id = " + user.id + ") OR ((d.state_id = 18 OR d.state_id = 20) AND d.custom_assignee_id = " + user.id + ")))";
  var cond2 = "d.is_canceled = 0 AND (d.state_id = 9 AND d.designer_id = " + user.id + ")";
  var cond3 = "d.is_canceled = 0 AND (((d.state_id = 19 OR d.state_id = 21 ) AND d.custom_assigner_id = " + user.id + ") OR ((d.state_id = 18 OR d.state_id = 20) AND d.custom_assignee_id = " + user.id + ")))";

  if (smryGui) {
    var smryArgs = [ { name: smryGui.allTasks.id, cond: cond1 },
                     { name: smryGui.complete.id, cond: cond2 },
                     { name: smryGui.personal.id, cond: cond3 }
                   ];
    return smryArgs;
  }
  return null;
};

PPublishUtils.mailerSmryArgs = function(smryGui) {
  var cond1 = "d.is_canceled = 0 AND (d.state_id = 16 OR d.state_id = 17)";
  var cond2 = "d.is_canceled = 0 AND (d.state_id = 16)";
  var cond3 = "d.is_canceled = 0 AND (d.state_id = 17)";
  if (smryGui) {
    var smryArgs = [ { name: smryGui.allTasks.id, cond: cond1 },
                     { name: smryGui.unreceived.id, cond: cond2 },
                     { name: smryGui.received.id, cond: cond3 }
                   ];
    return smryArgs;
  }
  return null;
};


/**
 * 清理界面对象
 */
PPublishUtils.clear = function(gui) {
  for (key in gui) {
    var elem = gui[key];
    if (!elem) {
      continue;
    }
    if (elem.constructor.toString().indexOf('Array') == -1 && elem.constructor.toString().indexOf('Object') == -1) {
      if (!elem._protected) {
        DOMUtils.removeChildElements(elem);
      }
    } else {
      this.clear(elem);
    }
  }
};

/**
 * 删除一个对象的所有id
 */
PPublishUtils.renew = function(arg) {
  var obj = MiscUtils.clone(arg);
  for (var key in obj) {
    if (typeof(obj[key]) == 'object' || typeof(obj[key]) == 'array') {
      obj[key] = this.renew(obj[key]);
    }
    if (key == 'id') {
      delete obj[key];
    }
  }
  return obj;
};
/**
 * 创建新的photographer
 */
PPublishUtils.photographer = function(object) {
  var obj = MiscUtils.clone(object);
  var p = pz_user.instance();
  p.track_id = MiscUtils.unique();
  p.account = this.randomChar(6);
  p.name = obj.name;
  p.password = '1234';
  p.type = obj.type;
  p.email = obj.email;
  p.branch = obj.branch;
  p.photographer_old_id = obj.id;
  return p;
};
/**
 * 根据id得到obj
 */
PPublishUtils.object = function(id, objects) {
  for (var i = 0, il = objects.length; i < il; i++) {
    var object = objects[i];
    if (object.id == id) {
      return object;
    }
  }
  return null;
};

/**
 * 
 */
PPublishUtils.customerId = function(customer_id) {
  if (customer_id < 100000 && customer_id >= 10000) {
    customer_id = customer_id;
  } else if (customer_id < 10000 && customer_id >= 1000) {
    customer_id = '0' + customer_id;
  } else if (customer_id < 1000 && customer_id >= 100) {
    customer_id = '00' + customer_id;
  } if (customer_id < 100 && customer_id >= 10) {
    customer_id = '000' + customer_id;
  } if (customer_id < 10 && customer_id > 0) {
    customer_id = '0000' + customer_id;
  }
  return customer_id;
};

PPublishUtils.customerName = function(bride, groom) {
  var name = '';
  
  if (bride != '' && groom != '') {
    name = bride + ' & ' + groom;
  } else if (bride != '' && groom == '') {
    name = bride;
  } else if (bride == '' && groom != '') {
    name = groom;
  } else {
    name = '-'
  }
  
  return name;
};
/**
 * 把任务转换成模式和状态的数据库询问语句
 */
PPublishUtils.lookup = function(text, models, states) {
  var sql = '';
  for (var i = 0, il = models.length; i < il; i++) {
    var model = models[i];
    for (var j = 0, jl = states.length; j < jl; j++) {
      var state = states[j];
      if (state.name.indexOf(text) != -1) {
        sql += ((sql == '') ? '(' : ' OR ') + '(t.model_id =\'' + model.id + '\' AND t.state_id = \'' + state.id + '\' AND t.is_canceled = 0)';
      }
    }
  }
  sql += ((sql == '') ? '1 != 1' : ')');
  return sql;
};

PPublishUtils.text = function(task, event, models, states, transitions, extra) {
  var m, s, t = [];
  for (var i = 0, il = models.length; i < il; i++) {
    var tmp = models[i];
    if (tmp.id == task.model_id) {
      m = tmp;
    }
  }
  if (!m) {
    return null;
  }
  for (var i = 0, il = states.length; i < il; i++) {
    var tmp = states[i];
    if (tmp.id == event.state_id && tmp.model_id == m.id) {
      s = tmp;
    }
  }
  if (!s) {
    return null;
  }
  for (var i = 0, il = transitions.length; i < il; i++) {
    var tmp = transitions[i];
    if (tmp.source_id == s.id && tmp.model_id == m.id) {
      return s.name+ ' (' + tmp.name + ') ' + extra;
    }
  }
  return extra;
};


PPublishUtils.track = function(task, track, models, states, transitions, extra) {
  var m, s, t = [];
  for (var i = 0, il = models.length; i < il; i++) {
    var tmp = models[i];
    if (tmp.id == task.model_id) {
      m = tmp;
    }
  }
  if (!m) {
    return null;
  }
  for (var i = 0, il = states.length; i < il; i++) {
    var tmp = states[i];
    if (tmp.id == track.state_id && tmp.model_id == m.id) {
      s = tmp;
    }
  }
  if (!s) {
    return null;
  }
  for (var i = 0, il = transitions.length; i < il; i++) {
    var tmp = transitions[i];
    if (tmp.source_id == s.id && tmp.model_id == m.id && tmp.id == track.transition_id) {
      return s.name+ ' (' + tmp.name + ') ' + extra;
    }
  }
  return extra;
};

PPublishUtils.randomChar = function(number) {
  var rand = '';
  for( var i = 0; i < number; i++) {
    rand += String.fromCharCode(Math.floor(Math.random() * 26) + "a".charCodeAt(0));
  }
  return rand;
};

PPublishUtils.processingSearch = function() {
  return { customerId: '',
           parcelNo: '',
           task: '',
           dateFrom: '',
           dateTo: ''
         };
};

PPublishUtils.sheetSearch = function() {
  return { customerNo: '',
           customerName: '',
           branch: '',
           photographer: '',
           culture: '',
           reception: '',
           ceremony: '',
           wedding_color: '',
           
           signed: true,
           nosigned: true,
           backup: true,
           nobackup: true,
           chosen: true,
           nochosen: true,
           asstretouch: true,
           noasstretouch: true,
           check: true,
           nocheck: true,
           adminretouch: true,
           noadminretouch: true,
           desarrange: true,
           nodesarrange: true,
           desretouch: true,
           nodesretouch: true,
           sort: true,
           nosort: true,
           author: true,
           noauthor: true,
           rauthor: true,
           norauthor: true,
           published: true,
           nopublished: true,
           
           weddingDateFrom: '',
           weddingDateTo: '',
           sentDateFrom: '',
           sentDateTo: '',
           receivedDateFrom: '',
           receivedDateTo: '',
           retouchedDateFrom: '',
           retouchedDateTo: '',
           publishedDateFrom: '',
           publishedDateTo: ''
          };
};

PPublishUtils.scoresheetSearch = function() {
  return { branch: '',
           culture: '',
           scoreFrom: '',
           scoreTo: '',
           weddingDateFrom: '',
           weddingDateTo: '',
           receivedDateFrom: '',
           receivedDateTo: ''
          };
};

PPublishUtils.branch = function(task) {
  var str = '';
  if (task.ub_akl == 1) {
    str += Constant.BRANCH_AKL + ', ';
  } 
  if (task.ub_syd == 1) {
    str += Constant.BRANCH_SYD + ', ';
  } 
  if (task.ub_mel == 1) {
    str += Constant.BRANCH_MEL + ', ';
  } 
  if (task.ub_nyc == 1) {
    str += Constant.BRANCH_NYC + ', ';
  } 
  if (task.ub_bne == 1) {
    str += Constant.BRANCH_BNE + ', ';
  } 
  if (task.ub_xtrao == 1) {
    str += Constant.BRANCH_XTRAO + ', ';
  }

  str = str.substr(0, str.length - 2);
  return str;
};

PPublishUtils.branchShow = function(user) {
  var str = '';
  if (user.is_akl == 1) {
    str += Constant.BRANCH_AKL + ', ';
  } 
  if (user.is_syd == 1) {
    str += Constant.BRANCH_SYD + ', ';
  } 
  if (user.is_mel == 1) {
    str += Constant.BRANCH_MEL + ', ';
  } 
  if (user.is_nyc == 1) {
    str += Constant.BRANCH_NYC + ', ';
  } 
  if (user.is_bne == 1) {
    str += Constant.BRANCH_BNE + ', ';
  } 
  if (user.is_xtrao == 1) {
    str += Constant.BRANCH_XTRAO + ', ';
  }

  str = str.substr(0, str.length - 2);
  return str;
};
PPublishUtils.diff = function(oldobj, newobj) {
  var diff = false;
  
  if (oldobj.is_wedding != newobj.is_wedding) {
    diff = true;
  } else if (oldobj.bride_name != newobj.bride_name) {
    diff = true;
  } else if (oldobj.groom_name != newobj.groom_name) {
    diff = true;
  } else if (oldobj.culture1 != newobj.culture1) {
    diff = true;
  } else if (oldobj.culture2 != newobj.culture2) {
    diff = true;
  } else if (oldobj.ceremony != newobj.ceremony) {
    diff = true;
  } else if (oldobj.reception != newobj.reception) {
    diff = true;
  } else if (oldobj.wedding_color != newobj.wedding_color) {
    diff = true;
  } else if (oldobj.groom_name != newobj.groom_name) {
    diff = true;
  } else if (oldobj.photographer_id != newobj.photographer_id) {
    diff = true;
  } else if (oldobj.photographer2_id != newobj.photographer2_id) {
    diff = true;
  } else if (oldobj.designer_id != newobj.designer_id) {
    diff = true;
  } else if (oldobj.score != newobj.score) {
    diff = true;
  } else if (oldobj.is_asst_retouched != newobj.is_asst_retouched) {
    diff = true;
  } else if (oldobj.is_admin_retouched != newobj.is_admin_retouched) {
    diff = true;
  }
  return diff;
};
